﻿<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
	<title>注册程序之验证页面</title>
</head>
<body>
<%
	try {
		String text = "";
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		String name = request.getParameter("name");
		String sex = request.getParameter("gender");
		String email = request.getParameter("email");

		//加载数据驱动
		Class.forName("com.mysql.jdbc.Driver");
		//创建连接
		Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/db_news2019","root","123456");
		//创建语句容器
		String sql1 = "select * from t_user where uid=?";
		PreparedStatement stmt1 = conn.prepareStatement(sql1);
		stmt1.setObject(1,id);
		ResultSet rs = stmt1.executeQuery();

		if ("".equals(password1) || "".equals(password2) || "".equals(id) || "".equals(name)) {
			text = "注册信息不完整！";
			response.sendRedirect("reg_failure.jsp");
		}else if(rs.next()) {
			text = "用户ID已存在！";
			response.sendRedirect("reg_failure.jsp");
		} else if (!password1.equals(password2)) {
			text = "两次输入密码不一致！";
			response.sendRedirect("reg_failure.jsp");
		} else {
			String sql2 = "insert t_user values (?,?,?,?,?,?)";
			PreparedStatement stmt2 = conn.prepareStatement(sql2);
			stmt2.setObject(1,id);
			stmt2.setObject(2,name);
			stmt2.setObject(3,password1);
			stmt2.setObject(4,sex);
			stmt2.setObject(5,email);
			stmt2.setObject(6,new java.util.Date());
			int result = stmt2.executeUpdate();
			response.sendRedirect("reg_success.jsp");
			stmt2.close();
		}
		session.setAttribute("text",text);
		session.setAttribute("name",name);
		rs.close();
		stmt1.close();
		conn.close();
	} catch (Exception e) {
		out.print("数据添加失败！");
	}

%>
</body>
</html>
